package com.jzit168.library_base.ext

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback

/**
 *@Desc: Activity的扩展类
 *@Author: Android Developer
 *@Date: 2020/10/15
 *
 **/

//跳转函数
fun <T> Activity.start(clazz: Class<T>) {
    Intent(this, clazz).also {
        startActivity(it)
    }
}

//携带bundle的跳转函数
fun <T> Activity.start(clazz: Class<T>, bundle: Bundle?) {
    Intent(this, clazz).also {
        it.putExtra("bundle", bundle)
        startActivity(it)
    }
}

//toast函数
fun Activity.toast(msg: Any?) {
    Toast.makeText(this, "$msg", Toast.LENGTH_SHORT).show()
}

fun Activity.requestPermission(
    permissions: MutableList<String>,
    whyRequest: String,
    deniedWhy: String,
    request: RequestCallback
) {
    PermissionX.init(this as FragmentActivity)
        .permissions(permissions)
        .onExplainRequestReason { scope, deniedList ->
            scope.showRequestReasonDialog(deniedList, whyRequest, "知道了")
        }
        .onForwardToSettings { scope, deniedList ->
            scope.showForwardToSettingsDialog(deniedList, deniedWhy, "知道了")
        }
        .request { allGranted, grantedList, deniedList ->
            request.onResult(allGranted, grantedList, deniedList)
        }
}